<template>
	<view>
		<view class="topImg">废旧金属回收登记系统</view>
		<view class="formClass">
			<u-form :model="form" ref="uForm">
				<u-form-item label="账号:" prop="number" label-width='150' class="labelStyle" left-icon="account">
					<u-input v-model="form.number" placeholder="请输入手机号/用户名" placeholder-style="font-size:24rpx;" />
				</u-form-item>
				<u-form-item label="密码:" prop="password" label-width='150' class="labelStyle" left-icon="list-dot">
					<u-input v-model="form.password" placeholder="请输入登录密码" placeholder-style="font-size:24rpx;" />
				</u-form-item>
			</u-form>
		</view>
		<!-- <view class="register" @tap="toRegister">注册</view> -->
		<view class="login" @tap="toLogin">登录</view>
		<view class="bottomImg"></view>
	</view>
</template>

<script>
	import config from '@/utils/config.js';
	export default {
		data() {
			return {
				form: {
					number: '',
					password: '',
				},
				rules: {
					number: [{
							required: true,
							message: '请输入手机号/用户名',
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// this.$u.test.mobile()就是返回true或者false的
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					],
					password: [{
						required: true,
						message: '请输入登录密码',
						trigger: 'change'
					}]
				}
			}
		},
		onLoad() {

		},
		onShow() {
			this.getToken();
		},
		onReady() {
			// this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			getToken(){
				uni.request({
					url: config.host + '/huishou/pcLogin/getAppLoginInfo', //接口地址。
					data: {
						token: uni.getStorageSync('token')
					},
					method: 'POST',
					header: {
						'content-type': 'application/json;charset=UTF-8'
					},
					success: (res) => {
						if (Object.keys(res.data.item).length == 0) {
							uni.showToast({
								icon: 'none',
								title: '身份已过期，请重新登录！'
							});
							if(this.route != [] && this.route.length != 0){
								setTimeout(() => {
									uni.navigateTo({
										url: '../login/index'
									})
								}, 800);
							}
							
						} else {
							setTimeout(() => {
								uni.navigateTo({
									url: '../index/index'
								})
							}, 300);
						}
					},
					fail: (err) => {
						uni.showToast({
							icon: 'none',
							title: '网络异常,请稍后重试'
						});
					}
				});
			},
			
			// toRegister() {
			// 	uni.navigateTo({
			// 		url: "../register/index",
			// 	})
			// },
			toLogin() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						uni.request({
							url: config.host + '/huishou/pcLogin/loginOn', //接口地址。
							data: {
								userName: this.form.number,
								password: this.form.password,
							},
							method: 'POST',
							header: {
								'content-type': 'application/json;charset=UTF-8'
							},
							success: (res) => {
								if (res.data.code == 200) {
									uni.setStorage({
										key: 'token',
										data: res.data.item.user.token,
									});
									uni.setStorage({
										key: 'userId',
										data: res.data.item.user.id,
									});
									uni.setStorage({
										key: 'identity',
										data: res.data.item.user.identity,
									});
									uni.setStorage({
										key: 'userName',
										data: res.data.item.user.name,
									});
									uni.showToast({
										title: '登录成功!',
										icon: 'none'
									})
									setTimeout(() => {
										uni.navigateTo({
											url: '../index/index'
										})
									}, 300);
									this.form.number = "";
									this.form.password = "";
								} else {
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									})
								}
							},
							fail: (err) => {
								uni.showToast({
									icon: 'none',
									title: '网络异常,请稍后重试'
								});
							}
						});
					} else {

					}
				});
			}
		}
	}
</script>

<style lang="scss">
	@import url("./css/index.css");
</style>
